//
//  ViewController.swift
//  RXLogin
//
//  Created by Pro on 2019/7/26.
//  Copyright © 2019 Pro. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

fileprivate let minNameLen = 5
fileprivate let minPasswordLen = 6

class ViewController: UIViewController {
    @IBOutlet weak var userNameTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    @IBOutlet weak var userNameVailLab: UILabel!
    @IBOutlet weak var passwordVailLab: UILabel!
    @IBOutlet weak var loginBtn: UIButton!
    
    let disposebeg = DisposeBag()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let userNameValid = userNameTF.rx.text.orEmpty
            .map { (text) -> Bool in
                return text.count >= minNameLen
        }
        
     userNameValid.bind(to: userNameVailLab.rx.isHidden)
        .disposed(by: disposebeg)
        
    userNameValid.bind(to: passwordTF.rx.isEnabled)
        .disposed(by: disposebeg)
        
        
        let paswordValid = passwordTF.rx.text.orEmpty
            .map { (text) -> Bool in
                return text.count >= minPasswordLen
        }
        
        paswordValid.bind(to: passwordVailLab.rx.isHidden)
        .disposed(by: disposebeg)
        
        Observable.combineLatest(userNameValid,paswordValid){ $0 && $1}
        .bind(to: loginBtn.rx.isEnabled)
        .disposed(by: disposebeg)
        
        loginBtn.rx.tap.subscribe(onNext: { () in
            print("点击了一下")
            self.userNameTF.text = ""
            self.passwordTF.text = ""
        })
        .disposed(by: disposebeg)
        
        
        
        
        
        
        
        
    }


}

